var express = require('express');
var router = express.Router();
var {query} = require('../../utils/db')
//文章列表页
router.get('/list',(req,res)=>{
    var sql = `select aid,title,cname,author,click from article,category where article.cid = category.cid order by aid desc`

    query(sql,(data)=>{
        res.render('admin/article/list',{articles:data})

    })
})

// 文章添加模板显示
router.get('/add',(req,res)=>{
    //获得当前cid 
    var cid = req.query.cid
    console.log(cid)
    // 获取所有栏目数据
    var sql = 'select * from category'
    query(sql,(categories)=>{
        res.render('admin/article/add',{categories:categories,cid:cid})
    })
})

// 文章填加数据保存
router.post('/add',(req,res)=>{
    var title = req.body.title
    var cid = parseInt(req.body.cid)
    var author = req.body.author
    var content = req.body.content
    var time = req.body.time
    var brife = req.body.brife
    var sql = `insert into article (title,cid,author,content,time,brife) values ('${title}',${cid},'${author}','${content}','${time}','${brife}')`
    query(sql,(data)=>{
        req.flash('success','添加成功')
        res.redirect('/admin/article/list')
    })
})


module.exports = router;